package com.zking.forward.view;

import com.zking.dao.ViewDao;
import com.zking.entity.View;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet("/ViewSelectByPage2")
public class ViewSelectByPage2 extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //处理请求的编码，解决中文乱码
        request.setCharacterEncoding("utf-8");
        //处理响应的编码 ，解决中文乱码
        response.setContentType("text/html;charset=utf-8");

        System.out.println("ViewSelectByPage===进来");
        int pageIndex = 1;
        int pageSize =4;
        String temp = request.getParameter("pageIndex");
        if(temp != null){
            pageIndex = Integer.parseInt(temp);
        }

        List<View> list = ViewDao.getNewsByPage(pageIndex, pageSize);
        //System.out.println(list);
        int maxPage = ViewDao.getMaxPage(pageSize);

        request.setAttribute("list", list);
        request.setAttribute("pageIndex", pageIndex);
        request.setAttribute("maxPage", maxPage);

        request.getRequestDispatcher("guestbook.jsp").forward(request, response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }
}